﻿


Public Class frmImportCongTyTuFileExce
    Dim dt As DataTable = New DataTable()
    Private Sub ImportTừFileExcelToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ImportTừFileExcelToolStripMenuItem.Click
        MessageBox.Show("Bạn phải tạo ra file .xls có cấu như trên hình, trước khi thực hiên nếu không quá trình Import sẽ bị lỗi")

        Dim SaveFileDialog1 As SaveFileDialog = New SaveFileDialog()
        SaveFileDialog1.Filter = "Export (*.xls)|*.xls"
        SaveFileDialog1.ShowDialog()
        Dim File_name As String = SaveFileDialog1.FileName

        ExcelProvider.file_name = File_name

        Try
            Dim sql As String = "Select * From [Sheet1$]"
            dt = ExcelProvider.DataAdapter(sql)
            dgvNha.DataSource = dt

        Catch ex As Exception
            MessageBox.Show("Lỗi")
        End Try
    End Sub

    Private Sub LưuToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LưuToolStripMenuItem.Click
        Dim ma As String = ""
        Try

            For i As Integer = 0 To dt.Rows.Count - 1

                Dim ct As CongTyDTO = New CongTyDTO()
                Dim t As Long = 0
                ma = dt.Rows(i)(0).ToString()
                ct.TenCongTy = dt.Rows(i)(1).ToString()
                ct.DienThoai = dt.Rows(i)(2).ToString()
                ct.MaQuan = dt.Rows(i)(3).ToString()
                ct.Email = dt.Rows(i)(4).ToString()
                ct.DiaChi = dt.Rows(i)(5).ToString()
                ct.NgayTiepNhan = Date.Parse(dt.Rows(i)(6).ToString())
               
                CongTyBUS.InsertCongTyBUS(ct)

            Next
            MessageBox.Show("Insert xuống cơ sở dữ liệu thành công")
            dgvNha.DataSource = CongTyBUS.GetAllCongTyBUS()

        Catch ex As Exception
            MessageBox.Show("Insert bị lỗi từ dòng có mã : " & ma.ToString())
        End Try
    End Sub
End Class